package org.takeback.chat.lottery.listeners;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.takeback.chat.entity.Message;
import org.takeback.chat.lottery.Lottery;
import org.takeback.chat.lottery.LotteryDetail;
import org.takeback.chat.service.Game04Service;
import org.takeback.chat.service.UserService;
import org.takeback.chat.store.room.LotteryFactory;
import org.takeback.chat.store.room.Room;
import org.takeback.chat.store.user.User;
import org.takeback.chat.utils.MessageUtils;
import org.takeback.chat.utils.NumberUtil;

@Component("G04")
public class G04 extends DefaultGameListener {
	@Autowired
	Game04Service game04Service;
	
	@Autowired
	UserService userService;

	public boolean onBeforeOpen(Integer uid, Lottery lottery) throws GameException {
		Room r = (Room) this.roomStore.get(lottery.getRoomId());
		Double money = getDeposit(r, lottery);
		if (lottery.getSender().equals(uid)) {
			return true;
		}
		if (this.lotteryService.moneyDown(uid, money) < 1) {
			throw new GameException(500, "金额不能少于" + money + "元,请及时充值!");
		}
		return true;
	}

	public void onOpen(Lottery lottery, LotteryDetail lotteryDetail) throws GameException {
		User opener = (User) this.userStore.get(lotteryDetail.getUid());
		Room room = (Room) this.roomStore.get(lottery.getRoomId());
		User sender = (User) this.userStore.get(lottery.getSender());
		Integer tailPoint = NumberUtil.getTailPoint(lotteryDetail.getCoin());
		String raidStr = lottery.getDescription().charAt(lottery.getDescription().length() - 1) + "";
		Integer raidPoint = Integer.valueOf(raidStr);

		String msg = opener.getNickName() + " 抢了你的红包,幸运躲过了地雷!";
		if (lottery.getSender().equals(lotteryDetail.getUid())) {
			this.lotteryService.moneyUp(opener.getId(), Double.valueOf(lotteryDetail.getCoin().doubleValue()));
			this.game04Service.saveDetail(lottery, lotteryDetail,
					Double.valueOf(lotteryDetail.getCoin().doubleValue()));
		} else if (lotteryDetail.getUid().equals(room.getUnDead())) {
			this.lotteryService.moneyUp(opener.getId(),
					Double.valueOf(getDeposit(room, lottery).doubleValue() + lotteryDetail.getCoin().doubleValue()));
			this.game04Service.saveDetail(lottery, lotteryDetail,
					Double.valueOf(lotteryDetail.getCoin().doubleValue()));
		} else if (tailPoint.equals(raidPoint)) {
			msg = "<span style='color:#F89C4C'>" + opener.getNickName() + "</span>不幸踩中你埋下的地雷!</span>";
			this.lotteryService.moneyUp(sender.getId(), getDeposit(room, lottery));
			this.lotteryService.moneyUp(opener.getId(), Double.valueOf(lotteryDetail.getCoin().doubleValue()));
			this.game04Service.saveDetail(lottery, lotteryDetail,
					Double.valueOf(lotteryDetail.getCoin().doubleValue() - getDeposit(room, lottery).doubleValue()));
		} else {
			this.lotteryService.moneyUp(opener.getId(),
					Double.valueOf(lotteryDetail.getCoin().doubleValue() + getDeposit(room, lottery).doubleValue()));
			this.game04Service.saveDetail(lottery, lotteryDetail,
					Double.valueOf(lotteryDetail.getCoin().doubleValue()));
		}
		Message notice = new Message("TXT_SYS", Integer.valueOf(0), msg);
		MessageUtils.send(lottery.getSender(), room, notice);
	}

	public boolean onBeforeRed(LotteryFactory.DefaultLotteryBuilder builder) throws GameException {
		Room room = builder.getRoom();
		int master = room.getMaster().intValue();
		int sender = builder.getSender();
		User user = (User) this.userStore.get(Integer.valueOf(sender));

		int maxSize = Integer.valueOf(getConifg(room.getId(), "conf_max_size")).intValue();
		int minSize = Integer.valueOf(getConifg(room.getId(), "conf_min_size")).intValue();
		if ((builder.getNumber() > maxSize) || (builder.getNumber() < minSize)) {
			throw new GameException(500, "房间限制红包个数:" + minSize + "-" + maxSize + "个");
		}
		Double maxMoney = Double.valueOf(getConifg(room.getId(), "conf_max_money"));
		Double minMoney = Double.valueOf(getConifg(room.getId(), "conf_min_money"));
		if ((builder.getMoney().doubleValue() > maxMoney.doubleValue())
				|| (builder.getMoney().doubleValue() < minMoney.doubleValue())) {
			throw new GameException(500, "红包金额限制:" + minMoney + "-" + maxMoney);
		}
		builder.setType("2");

		int expired = Integer.valueOf(getConifg(room.getId(), "conf_expired")).intValue();

		String raid = builder.getDescription();
		Integer raidPoint;
		try {
			raidPoint = Integer.valueOf(raid);
		} catch (Exception e) {
			throw new GameException(500, "雷点数为0-9的个位数字");
		}
		if ((raidPoint.intValue() > 9) || (raidPoint.intValue() < 0)) {
			throw new GameException(500, "雷点数为0-9的个位数字");
		}
		double water = getWater(room, Double.valueOf(builder.getMoney().doubleValue())).doubleValue();
		builder.setMoney(NumberUtil.round(builder.getMoney().subtract(new BigDecimal(water))));
		//抽完水后 抽佣金
		Double yongjin = userService.shareYongjin(sender, builder.getMoney().doubleValue(), room);
		System.out.println("G04  分享佣金啦 " + yongjin);
		builder.setMoney(NumberUtil.round(builder.getMoney().subtract(new BigDecimal(yongjin))));
		double cutMoney = builder.getMoney().doubleValue();
		builder.setDescription(builder.getMoney() + "金币/雷" + raidPoint);
		builder.setExpiredSeconds(Integer.valueOf(expired));
		int affected = this.lotteryService.moneyDown(Integer.valueOf(sender), Double.valueOf(cutMoney));
		if (affected == 0) {
			throw new GameException(500, "金额不足!余额必须大于" + cutMoney);
		}
		return true;
	}

	public void onRed(LotteryFactory.DefaultLotteryBuilder builder) throws GameException {
		Lottery l = builder.build();
		Room room = (Room) this.roomStore.get(builder.getRoomId());
		room.setStatus("1");
		MessageUtils.broadcast(room, new Message("gameBegin", null));
		if ((room.getUnDead() != null) && (room.getUnDead().intValue() > 0)) {
			BigDecimal b = l.fakeOpen(room.getUnDead().intValue());
			this.lotteryService.moneyUp(room.getUnDead(), Double.valueOf(b.doubleValue()));
		}
	}

	public void onFinished(Lottery lottery) throws GameException {
		Room room = (Room) this.roomStore.get(lottery.getRoomId());
		room.setStatus("0");
		MessageUtils.broadcast(room, new Message("gameOver", null));
		this.lotteryService.setLotteryFinished(lottery.getId());
		double water = getWater(room, Double.valueOf(lottery.getMoney().doubleValue())).doubleValue();
		this.lotteryService.waterUp(room.getId(), Double.valueOf(water));
		Message rmsg = new Message("TXT_SYS", Integer.valueOf(0), "游戏包已被抢光!");
		MessageUtils.broadcast(room, rmsg);
		this.game04Service.setMasterMonitorData(lottery);

		this.lotteryService.setBackWater(lottery.getSender(), Double.valueOf(water));
	}

	public void processExpireEvent(Lottery lottery) throws GameException {
		Room room = (Room) this.roomStore.get(lottery.getRoomId());
		if (lottery.getRestMoney().doubleValue() > 0.0D) {
			double water = getWater(room, Double.valueOf(lottery.getMoney().doubleValue())).doubleValue();
			double money = lottery.getRestMoney().doubleValue();
			if (lottery.getRestNumber() != lottery.getNumber()) {
				this.lotteryService.waterUp(room.getId(), Double.valueOf(water));
			}
			this.lotteryService.moneyUp(lottery.getSender(), Double.valueOf(money));
		}
		Message rmsg = new Message("TXT_SYS", Integer.valueOf(0), "地雷包已过期!");
		MessageUtils.broadcast(room, rmsg);
		this.game04Service.setMasterMonitorData(lottery);
	}

	private Double getDeposit(Room r, Lottery lottery) {
		Double rate = r.getFeeAdd();

		Double types = Double.valueOf(1.0D);
		if (r.getProperties().containsKey("conf_rate")) {
			types = Double.valueOf(r.getProperties().get("conf_rate").toString());
		}
		return Double.valueOf(lottery.getMoney().doubleValue() / (1.0D - rate.doubleValue()) * types.doubleValue());
	}

	private Double getWater(Room r, Double money) {
		Double rate = Double.valueOf(r.getFeeAdd() != null ? r.getFeeAdd().doubleValue() : 0.0D);
		if (rate.equals(Integer.valueOf(0))) {
			return Double.valueOf(0.0D);
		}
		return Double.valueOf(money.doubleValue() * rate.doubleValue());
	}
}
